<template>
  <a-modal
    :title="title"
    :width="640"
    :visible="visible"
    :confirmLoading="loading"
    @ok="_submit"
    @cancel="_cancel">
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <!-- 检查是否有 id 并且大于0，大于0是修改。其他是新增，新增不显示主键ID -->
        <a-form-item v-show="false" label="主键ID">
          <a-input v-decorator="['custMagerId', { initialValue: 0 }]" disabled />
        </a-form-item>
        <a-form-item label="加盟商">
          <a-select
            placeholder="请选择加盟商"
            v-decorator="['franchiseeId', { rules: [{ required: true, message: '请选择加盟商，角色不能为空！' }] }]"
          >
            <a-select-option :value="item.franchiseeId" v-for="item in franchiseeSelList" :key="item.franchiseeId">
              {{ item.franchiseeName }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="姓名">
          <a-input v-decorator="['userName', { rules: [{ required: true, message: '姓名不能为空!' }] }]" />
        </a-form-item>
        <a-form-item label="手机号码">
          <a-input
            :maxLength="11"
            v-decorator="['userMobile', { rules: [{ required: true, message: '手机号码不能为空!' }] }]"
          />
        </a-form-item>
        <a-form-item label="Logo">
          <a-upload
            name="logo"
            list-type="picture-card"
            class="avatar-uploader"
            :show-upload-list="false"
            :before-upload="beforeUpload"
            :customRequest="customRequest"
            v-decorator="['logoUrl']"
          >
            <img class="bannerImg" v-if="logoUrl" :src="logoUrl" alt="logoUrl" />
            <div v-else>
              <a-icon :type="uploadLoading1 ? 'loading' : 'plus'" />
              <div class="ant-upload-text">Upload</div>
            </div>
          </a-upload>
        </a-form-item>
        <a-form-item label="职称">
          <a-input v-decorator="['userTitle']" />
        </a-form-item>
        <a-form-item label="微信号" v-decorator="['wechat']">
          <a-input />
        </a-form-item>
        <a-form-item label="二维码">
          <a-upload
            name="qrCode"
            list-type="picture-card"
            class="avatar-uploader"
            :show-upload-list="false"
            :before-upload="beforeUpload"
            :customRequest="customRequest"
            v-decorator="['qrCode']"
          >
            <img class="bannerImg" v-if="qrCode" :src="qrCode" alt="qrCode" />
            <div v-else>
              <a-icon :type="uploadLoading2 ? 'loading' : 'plus'" />
              <div class="ant-upload-text">Upload</div>
            </div>
          </a-upload>
        </a-form-item>
        <a-form-item label="简介">
          <a-textarea v-decorator="['introduce']" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { uploadFile, getFranchiseeSelList } from '@/api/common'
// 表单字段
const fields = [
  'custMagerId',
  'franchiseeId',
  'logoUrl',
  'userName',
  'userMobile',
  'userTitle',
  'wechat',
  'qrCode',
  'introduce'
]

export default {
  props: {
    title: {
      type: String,
      required: true
    },
    visible: {
      type: Boolean,
      required: true
    },
    loading: {
      type: Boolean,
      default: () => false
    },
    model: {
      type: Object,
      default: () => null
    },
    checkedKeys: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data () {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 }
      }
    }
    return {
      uploadLoading1: false,
      uploadLoading2: false,
      logoUrl: '',
      qrCode: '',
      franchiseeSelList: [],
      form: this.$form.createForm(this)
    }
  },
  methods: {
    /**
     * bannerType切换
     */
    _bannerTypeChange (bannerType) {
      this.bannerType = bannerType
    },
    /**
     * 提交表单
     */
    _submit: function () {
      this.$emit('ok')
    },
    /**
     * 取消
     */
    _cancel: function () {
      this.$emit('cancel')
    },
    beforeUpload (file) {
      const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png'
      if (!isJpgOrPng) {
        this.$message.error('You can only upload JPG file!')
      }
      const isLt2M = file.size / 1024 / 1024 < 2
      if (!isLt2M) {
        this.$message.error('Image must smaller than 2MB!')
      }
      return isJpgOrPng && isLt2M
    },
    customRequest (options) {
      console.log(options)
      const params = new FormData()
      params.append('file', options.file)
      params.append('uploadType', 'custContract')
      if (options.filename === 'logo') {
        this.uploadLoading1 = true
        uploadFile(params).then((res) => {
          this.logoUrl = res.url
          this.form.setFieldsValue({
            logoUrl: res.url
          }).catch((e) => {})
          this.uploadLoading1 = false
        })
      } else {
        this.uploadLoading2 = true
        uploadFile(params).then((res) => {
          this.qrCode = res.url
          this.form.setFieldsValue({
            qrCode: res.url
          })
          this.uploadLoading2 = false
        }).catch((e) => {})
      }
    }
  },
  created () {
    // 防止表单未注册
    fields.forEach((v) => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      this.logoUrl = this.model?.logoUrl ? this.model.logoUrl : ''
      this.qrCode = this.model?.qrCode ? this.model.qrCode : ''
      if (this.model) {
        this.form.setFieldsValue(pick(this.model, fields))
      }
    })
  },
  mounted () {
    getFranchiseeSelList({}).then((res) => {
      this.franchiseeSelList = res.dataList
    }).catch((e) => {})
  }
}
</script>
<style lang="less" scoped>
.avatar-uploader > .ant-upload {
  width: 128px;
  height: 128px;
}
.ant-upload-select-picture-card i {
  font-size: 32px;
  color: #999;
}

.ant-upload-select-picture-card .ant-upload-text {
  margin-top: 8px;
  color: #666;
}
.bannerImg {
  width: 100%;
  height: 100%;
}
</style>
